<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="85px"
    >
      <el-form-item label="公司名称:" prop="compName">
        <el-input
          v-model="queryParams.compName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务员:" prop="collaborator">
        <el-input
          v-model="queryParams.collaborator"
          placeholder="请输入业务员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位:" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入职位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属大区:" prop="maxDepartment">
        <el-input
          v-model="queryParams.maxDepartment"
          placeholder="请输入所属大区"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门:" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入所属部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="kqAllOrderList"
      height="650"
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="公司名称" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link
            target="_blank"
            :href="
              'https://www.tianyancha.com/search?key=' + scope.row.compName
            "
          >
            {{ scope.row.compName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" show-overflow-tooltip />
      <el-table-column prop="dealNum" label="成交额(万)" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.dealNum / 10000 }}
        </template>
      </el-table-column>
      <el-table-column label="成交时间" align="center" prop="orderDate" show-overflow-tooltip />
      <el-table-column label="业务员" align="center" prop="collaborator" show-overflow-tooltip />
      <el-table-column label="职位" align="center" prop="position" show-overflow-tooltip />
      <el-table-column label="所属大区" align="center" prop="maxDepartment" show-overflow-tooltip />
      <el-table-column label="所属部门" align="center" prop="department" show-overflow-tooltip />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  getAllKqOrdersList,
  exportAllKqOrders
} from "@/api/work/allKqOrders";

export default {
  name: "Work",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      kqAllOrderList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        compName: undefined,
        collaborator: undefined
        // city: undefined,
        // district: undefined
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      getAllKqOrdersList(this.queryParams).then((res) => {
        this.kqAllOrderList = res.data.results;
        this.total = res.data.count;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有岗位数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportAllKqOrders(queryParams);
        })
        .then((response) => {
          this.download(response.data.file_url, response.data.name);
        });
    }
  }
};
</script>
